const Utils = require("./Utils.js")

Utils.init()

var flag, region, num, direction

num = 0
flag = true
region = [950, 220, 670, 740]
// direction = "down"
direction = "up"

// 关闭好友协助申请
threads.start(() => {
  while (true) {
    sleep(30 * 1000)
    Utils.findPhotoTap("叉号", 0.8, [1100, 120, 140, 120])
  }
})

while (flag) {
  Utils.sleeps()
  if (
    Utils.findPictureTap("勋章空", region) ||
    Utils.findPhotoTap("勋章实", region)
  ) {
    let flagTempTwo = true
    while (flagTempTwo) {
      if (Utils.findPictureTap("进攻", 0.8)) {
        Utils.screenshot("进攻")
        Utils.sleeps()
        if (Utils.findPicture("进攻", 0.8)) {
          click(random(150, 290), random(120, 230))
          log("等待恢复中…………")
          Utils.ranWaitTime(100, 120)
        } else {
          let seconds = random(20, 30)
          Utils.waitTime(seconds)
          let times = 0
          let flagTempOne = true
          while (flagTempOne) {
            if (Utils.tapPicture("奖励")) {
              flagTempOne = false
            } else if (Utils.findPictureTap("失败")) {
              log("结界突破 - 失败")
              flagTempOne = false
            }
            times++
            log("已等待：" + (seconds + times) + "秒")
          }
        }
        num = 0
        flagTempTwo = false
      }
    }
  }
  num++
  log("检测待突破结界第 " + num + " 次")
  if (num === 5) {
    let swipeTimes = 0
    let flagTempThree = true
    while (flagTempThree && swipeTimes < 3) {
      direction === "down"
        ? swipe(random(640, 1600), 800, random(640, 1600), 210, 2000)
        : swipe(random(640, 1600), 210, random(640, 1600), 800, 2000)
      Utils.sleeps()
      if (
        Utils.findPicture("勋章空", region) ||
        Utils.findPhoto("勋章实", region)
      )
        flagTempThree = false
      else swipeTimes++
    }
  }
  num === 6 ? (log("结界突破 - 完成"), Utils.stopJB()) : ""
}
